package bean;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.biblioteca.interfaces.GerenciadorDeUsuario;
import br.com.biblioteca.model.Usuario;

@ManagedBean
@SessionScoped
public class LoginBEAN implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@EJB
	private GerenciadorDeUsuario gerenciadoDeUsuario;
	
	private Usuario user;
	private String login;
	private String senha;
	
	public LoginBEAN(){
		user = new Usuario();
	}
	
	public String fazerLogin(){
		user = gerenciadoDeUsuario.autenticar(login, senha);
		if (user != null ){
			senha="";
			if (user.getPerfil() == 1) {
				return "telaAdm.xhtml?faces-redirect=true";

			} else {
				return "telaUsuario.xhtml?faces-redirect=true";

			}
			
		} else {
			return "";
		}
	}
	
	public String sair(){
		FacesContext fc = FacesContext.getCurrentInstance();  
	    HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);  
	    session.invalidate(); 
		return "index.xhtml?faces-redirect=true";
	}
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}

	public Usuario getUser() {
		return user;
	}
	
	
}
